package com.eboss2.sbs.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.eboss2.sbs.R;
import com.eboss2.sbs.activity.ActivityPhotoviewList;
import com.eboss2.sbs.model.CommentList;
import com.eboss2.sbs.model.PhotoVO;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.tool.utils.utils.Base64Utils;
import com.tool.utils.view.MyGridView;
import com.tool.utils.view.RoundImageView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class AdapterComment extends BaseAdapter {

    @SuppressWarnings("unused")
    private Context mContext;
    private LayoutInflater mInflater;
    private List<CommentList> list;


    public AdapterComment(Context context, final List<CommentList> list) {
        this.mContext = context;
        this.list = list;
        mInflater = LayoutInflater.from(context);
       
    }

    

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list == null ? null : list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return list == null ? 0 : position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_comment, null);
            holder.iv = (ImageView) convertView.findViewById(R.id.img);
            holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
            holder.gridView = (MyGridView) convertView.findViewById(R.id.gridView);
            holder.l_title = (TextView) convertView.findViewById(R.id.l_title);
            holder.rb = (RatingBar) convertView.findViewById(R.id.rb_level);
            holder.tv_order_amt = (TextView) convertView.findViewById(R.id.tv_order_amt);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final CommentList vo = list.get(position);
        
        //头像
        ImageLoader.getInstance().displayImage( "/" +vo.getMember_icon(), holder.iv);

        //评分
        if (vo.getScore() == -1){
        	holder.rb.setRating(5);
        }else{
        	holder.rb.setRating(vo.getScore());
        }
        //内容
        holder.l_title.setText(vo.getComment_content());
        //评论的图片
        if (vo.getImgList().size()>0){
            holder.gridView.setVisibility(View.VISIBLE);
	        holder.gridView.setAdapter(new AdapterShopSaleComplete(mContext, vo.getImgList()));
	        
	        holder.gridView.setOnItemClickListener(new OnItemClickListener() {

	            @Override
	            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
	                List<PhotoVO> l = new ArrayList<PhotoVO>();
	                PhotoVO p = null;
	                for(int i=0;i<vo.getImgList().size();i++){
	                    p=new PhotoVO();
	                    p.setWebimgPath(vo.getImgList().get(i));
	                    l.add(p);
	                }
	                mContext.startActivity(new Intent(mContext, ActivityPhotoviewList.class).putExtra("paths", (Serializable)l).putExtra("position", position));
	                
	            }
	        });
	        
        }else{
            holder.gridView.setVisibility(View.GONE);
        }

       

        return convertView;
    }

   

    

    private static final class ViewHolder {

        ImageView iv;
        TextView tv_content;
        TextView tv_order_amt;
        TextView l_title;
        RatingBar rb;
        MyGridView gridView;
       
    }

}
